package com.smart4soft.stock.dao;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.smart4soft.stock.pojo.Product;
import com.smart4soft.stock.util.Page;

/**
 * A data access object (DAO) providing persistence and search support for
 * Product entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.smart4soft.stock.pojo.Product
 * @author MyEclipse Persistence Tools
 */

public class ProductDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(ProductDAO.class);
	// property constants
	public static final String PRODUCT_CODE = "productCode";
	public static final String PRODUCT_NAME = "productName";
	public static final String PRODUCT_UNIT = "productUnit";
	public static final String SPECIFICATION = "specification";
	public static final String COST_PRICE = "costPrice";
	public static final String UNIT_PRICE = "unitPrice";
	public static final String SALES_PRICE = "salesPrice";
	public static final String DESCRIPTION = "description";
	public static final String USE_STATUS = "useStatus";
	public static final String CREATE_NAME = "createName";
	public static final String UPDATE_NAME = "updateName";

	protected void initDao() {
		// do nothing
	}

	public void save(Product transientInstance) {
		log.debug("saving Product instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Product persistentInstance) {
		log.debug("deleting Product instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Product findById(java.lang.Long id) {
		log.debug("getting Product instance with id: " + id);
		try {
			Product instance = (Product) getHibernateTemplate().get(
					"com.smart4soft.stock.pojo.Product", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(Product instance) {
		log.debug("finding Product instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Product instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Product as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByProductCode(Object productCode) {
		return findByProperty(PRODUCT_CODE, productCode);
	}

	public List findByProductName(Object productName) {
		return findByProperty(PRODUCT_NAME, productName);
	}

	public List findByProductUnit(Object productUnit) {
		return findByProperty(PRODUCT_UNIT, productUnit);
	}

	public List findBySpecification(Object specification) {
		return findByProperty(SPECIFICATION, specification);
	}

	public List findByCostPrice(Object costPrice) {
		return findByProperty(COST_PRICE, costPrice);
	}

	public List findByUnitPrice(Object unitPrice) {
		return findByProperty(UNIT_PRICE, unitPrice);
	}

	public List findBySalesPrice(Object salesPrice) {
		return findByProperty(SALES_PRICE, salesPrice);
	}

	public List findByDescription(Object description) {
		return findByProperty(DESCRIPTION, description);
	}

	public List findByUseStatus(Object useStatus) {
		return findByProperty(USE_STATUS, useStatus);
	}

	public List findByCreateName(Object createName) {
		return findByProperty(CREATE_NAME, createName);
	}

	public List findByUpdateName(Object updateName) {
		return findByProperty(UPDATE_NAME, updateName);
	}

	public List findAll() {
		log.debug("finding all Product instances");
		try {
			String queryString = "from Product";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	
	public List getAllObject(String hql) {
		
		log.debug("finding all Product instances");
		try {
			List list = this.getHibernateTemplate().find(
					"from Product where 1=1 " + hql);
			return list;
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}
	

	public Product merge(Product detachedInstance) {
		log.debug("merging Product instance");
		try {
			Product result = (Product) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Product instance) {
		log.debug("attaching dirty Product instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Product instance) {
		log.debug("attaching clean Product instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public int findCount(String[] paramName, Object[] values, String where) {
		String hql = "SELECT COUNT(ProductID) FROM Product where 1=1 " + where;
		Query query = this.getSession().createQuery(hql);
		if (null != paramName && paramName.length > 0) {
			for (int i = 0; i < paramName.length; i++) {
				query.setParameter(i, values[i]);
			}
		}
		int count = ((Long) query.iterate().next()).intValue();
		return count;
	}

	public List findByWhere(final String where, String[] paramNames,
			Object[] values, final Page page) {
		log.debug("finding Product instance with property: " + where
				+ ", value: " + values);
		return this.getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				Query query = session.createQuery("from Product as model  where 1=1 "
						+ where);
				query.setFirstResult(page.getBeginIndex());
				query.setMaxResults(page.getEveryPage());
				return query.list();
			}
		});
	}

	public static ProductDAO getFromApplicationContext(ApplicationContext ctx) {
		return (ProductDAO) ctx.getBean("ProductDAO");
	}
}